#!/bin/env perl
#https://gist.github.com/atomd/2310502
use strict;
use warnings FATAL => 'all';
 
use Getopt::Long;
use MHA::DBHelper;


my (
    $command,          $ssh_user,        $orig_master_host, $orig_master_ip,
    $orig_master_port, $new_master_host, $new_master_ip,    $new_master_port,  $new_master_user,
  $new_master_password
);
 
my $vip = '10.47.7.201';  # Virtual IP
my $net_mask = "24";
my $key = "10306";
my $nic = "em1";
my $ssh_start_vip = "sudo /sbin/ifconfig $nic:$key $vip/$net_mask;/sbin/arping -b -c 2 -w 30 -I $nic $vip";
my $ssh_stop_vip = "sudo /sbin/ifconfig $nic:$key down";
 
GetOptions(
    'command=s'          => \$command,
    'ssh_user=s'         => \$ssh_user,
    'orig_master_host=s' => \$orig_master_host,
    'orig_master_ip=s'   => \$orig_master_ip,
    'orig_master_port=i' => \$orig_master_port,
    'new_master_host=s'  => \$new_master_host,
    'new_master_ip=s'    => \$new_master_ip,
    'new_master_port=i'  => \$new_master_port,
    'new_master_user=s'     => \$new_master_user,
    'new_master_password=s' => \$new_master_password,
);
 
exit &main();
 
sub main {
 
    print "\n\nIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===\n\n"; 
 
    if ( $command eq "stop" || $command eq "stopssh" ) {
 
        # $orig_master_host, $orig_master_ip, $orig_master_port are passed.
        # If you manage master ip address at global catalog database,
        # invalidate orig_master_ip here.
        my $exit_code = 1;
        eval {
            print "Disabling the VIP on old master: $orig_master_host \n";
            &stop_vip();
            $exit_code = 0;
        };
        if ($@) {
            warn "Got Error: $@\n";
            exit $exit_code;
        }
        exit $exit_code;
    }
    elsif ( $command eq "start" ) {
 
        # all arguments are passed.
        # If you manage master ip address at global catalog database,
        # activate new_master_ip here.
        # You can also grant write access (create user, set read_only=0, etc) here.
        my $exit_code = 10;
        eval {
          my $new_master_handler = new MHA::DBHelper();

          # args: hostname, port, user, password, raise_error_or_not
          $new_master_handler->connect( $new_master_ip, $new_master_port,$new_master_user, $new_master_password, 1 );

          ## Set read_only=0 on the new master
          $new_master_handler->disable_log_bin_local();
          print "Set read_only=0 on the new master.\n";
          $new_master_handler->disable_read_only();

          ## Creating an app user on the new master
          #print "Creating app user on the new master..\n";
          #FIXME_xxx_create_user( $new_master_handler->{dbh} );
          $new_master_handler->enable_log_bin_local();
          $new_master_handler->disconnect();

          # Update master ip on the catalog database, etc
          print "Enabling the VIP - $vip on the new master - $new_master_host \n";
            &start_vip();
            $exit_code = 0;
          };
          if ($@) {
            warn $@;
            exit $exit_code;
        }
        exit $exit_code; 
    }

    elsif ( $command eq "status" ) {
        print "Checking the Status of the script.. OK \n"; 
#        `ssh $ssh_user\@gentoo8 \" $ssh_start_vip \"`;
        exit 0;
    }
    else {
        &usage();
        exit 1;
    }
}
 
# A simple system call that enable the VIP on the new master 
sub start_vip() {
    `ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`;
}
# A simple system call that disable the VIP on the old_master
sub stop_vip() {
    `ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`;
}
 
sub usage {
    print
    "Usage: master_ip_failover --command=start|stop|stopssh|status --orig_master_host=host --orig_master_ip=ip --orig_master_port=port --new_master_host=host --new_master_ip=ip --new_master_port=port\n";
}
